/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2023-04-19 10:49:27
 */
//导入数据库实例 便于操作数据库
const db = require('../config/db')
//导入密码加密模块
const bcrypt = require('../dao/bcrypt')

//获取用户详情
exports.getUserDetail = (req, res) => {
    let userid = req.query.userid
    // console.log(req.headers.token);
    let sql = 'select userid,username,email,sex,birth,phone,`explain`,imgurl,registerTime from `user` where userid = ?';
    db.query(sql, userid, (err, result) => {
        if (err) return res.sendMsg(err, 500)
        if (result.length != 1) return res.sendMsg('获取用户详情失败', 400)
        else {
            res.sendMsg('success', 200, result)
        }
    })
}

//修改用户信息  修改邮箱，密码，用户名必须先要进行密码验证
exports.userUpdate = (data, res) => {
    console.log(data);
    let updatestr = null
    if (typeof (data.password) != 'undefined') { //有密码，需要匹配
        updatestr = 'select `password` from `user` where userid = ?';
        db.query(updatestr, data.userid, (err, result) => {
            if (err) return res.sendMsg(err, 500)
            if (result.length != 1) return res.sendMsg('出现失败', 400)
            else {
                //验证传入的旧密码是否与数据库中密码一致
                const pwdMatch = bcrypt.verification(data.password, result[0].password)
                if (pwdMatch) {  //密码验证成功
                    if (data.type == 'password') {  //修改的是密码
                        //新密码加密
                        let password = bcrypt.encryption(data.data)
                        console.log(password);
                        //修改数据库中该用户的密码
                        let sql = 'update user set `password` = ?  where userid = ?'
                        db.query(sql, [password, data.userid], (err, result) => {
                            if (err) return res.sendMsg(err, 500)
                            else return res.sendMsg('success', 200)
                        })
                    } else if (data.type == 'email') {  //若修改邮箱，需要先查询数据库中邮箱是否存在相同的
                        let str = 'select * from user where email = ?'
                        db.query(str, data.data, (err, result) => {
                            if (err) return res.sendMsg('数据库出现问题', 400)
                            //查询到新修改邮箱的数据，说明新邮箱已存在，则不能修改
                            if (result.length != 0) return res.sendMsg('已经存在该邮箱', 400)
                            else {  //邮箱未发现重复 可以修改
                                let sql = `update user set ${data.type} = ?  where userid = ?`
                                db.query(sql, [data.data, data.userid], (err, result) => {
                                    if (err) return res.sendMsg(err, 500)
                                    else return res.sendMsg('success', 200)
                                })
                            }
                        })
                    } else if (data.type == 'username') { //若修改用户名，需要先查询数据库中邮箱是否存在相同的
                        let str = 'select * from user where username = ?'
                        db.query(str, data.data, (err, result) => {
                            if (err) return res.sendMsg('数据库出现问题', 400)
                            //查询到新修改用户名的数据，说明新用户名已存在，则不能修改
                            if (result.length != 0) return res.sendMsg('已经存在该用户名', 400)
                            else {  //用户名未发现重复 可以修改
                                let sql = `update user set ${data.type} = ?  where userid = ?`
                                db.query(sql, [data.data, data.userid], (err, result) => {
                                    if (err) return res.sendMsg(err, 500)
                                    else return res.sendMsg('success', 200)
                                })
                            }
                        })
                    }
                    else {  //修改普通的字段
                        let sql = `update user set ${data.type} = ?  where userid = ?`
                        db.query(sql, [data.data, data.userid], (err, result) => {
                            if (err) return res.sendMsg(err, 500)
                            else return res.sendMsg('success', 200)
                        })
                    }
                } else {
                    res.sendMsg('密码验证失败', 400)
                }
            }
        })
    } else {   //不需要密码
        if (data.type == 'email' || data.type == 'username' || data.type == 'password') {
            return res.sendMsg('请先输入密码验证', 400)
        }
        if(data.type == 'explain'){
            let sql = "update user set `explain` = ? where userid = ?"
            db.query(sql, [data.data, data.userid], (err, result) => {
                if (err) return res.sendMsg(err, 500)
                else return res.sendMsg('success', 200)
            }) 
        }else {
            let sql = `update user set ${data.type} = ? where userid = ?`
            db.query(sql, [data.data, data.userid], (err, result) => {
                if (err) return res.sendMsg(err, 500)
                else return res.sendMsg('success', 200)
            })
        }
    }
}

//修改好友昵称
exports.friendMarkName = (data, res) => {
    let sql = 'update `friend` set nickname = ? where userid = ? and friendid = ?'
    db.query(sql, [data.name, data.uid, data.fid], (err, result) => {
        if (err) return res.sendMsg(err, 500)
        if (result.length == 0) return res.sendMsg(' ', 400)
        else {
            res.sendMsg('success', 200)
        }
    })
}

//获取好友昵称
exports.getFriendMarkName = (data, res) => {
    let sql = 'select nickname from `friend` where userid = ? and friendid = ?'
    db.query(sql, [data.uid, data.fid], (err, result) => {
        if (err) return res.sendMsg(err, 500)
        if (result.length == 0) return res.sendMsg('找不到该好友', 400)
        else {
            res.sendMsg('success', 200, result)
        }
    })
}